package cn.zd.controller;

import cn.zd.pojo.ForeignExchangeEntity;
import cn.zd.service.ForeignExchangeService;
import cn.zd.util.CSVUtils;

import java.util.*;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;

import java.io.IOException;
import java.util.stream.Collectors;

import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author zhangdong
 * @version 1.0.0
 * <p>
 * @ClassName IndexController.java
 * @createTime 2023/03/25 23:46:00
 */
@Controller public class IndexController {

    @Autowired private ForeignExchangeService foreignExchangeService;

    @GetMapping() public String index(Model model) {

        List<ForeignExchangeEntity> collects = foreignExchangeService.queryAll();

        model.addAttribute("collects", collects);

        return "index";
    }

    @GetMapping("/map1") public String map1(HttpServletRequest request) {
        List<ForeignExchangeEntity> collects = foreignExchangeService.queryAll();
        StringBuffer sbf = new StringBuffer();
        collects.forEach(t -> {
            Object[] strings = {t.getCreateAt(), 10, 11, 22, 22, 10, 11, 22, 11, "-"};
            sbf.append(Arrays.toString(strings)).append(",");
        });

        List<ForeignExchangeEntity> collects1 = foreignExchangeService.query4();
        Set<String> timeSet = collects1.stream().map(ForeignExchangeEntity::getCreateAt).collect(Collectors.toSet());
        StringBuffer timeArray = new StringBuffer();
        timeSet.forEach(t -> {
            timeArray.append(t).append(",");
        });
        request.setAttribute("collects", sbf);
        request.setAttribute("time", timeArray);
        return "map";
    }

    @GetMapping("/map") public String map(HttpServletRequest request) {
        List<ForeignExchangeEntity> collects = foreignExchangeService.queryLikeTitle("1、金荣中国");

        List<String> timeSet = collects.stream().sorted((o1, o2) -> {
            return o1.getCreateAt1(o1.getCreateAt()).compareTo(o2.getCreateAt1(o2.getCreateAt()));
        }).map(ForeignExchangeEntity::getCreateAt).collect(Collectors.toList());
        StringBuffer timeArray = new StringBuffer();

        timeSet.forEach(t -> {
            timeArray.append(t).append(",");
        });
        String substring = timeArray.substring(0, timeArray.length() - 1);
        request.setAttribute("time", substring);
        return "map";
    }

    @GetMapping("/spider") public String spider(Model model) throws Exception {

        List<ForeignExchangeEntity> collects = foreignExchangeService.queryAll();
        if (!CollectionUtils.isEmpty(collects)) {
            foreignExchangeService.init();
        }

        String url = "https://www.longfajr.com/info/299346.html";

        Document doc = Jsoup.connect(url).get();

        int[] indexList = new int[] {8, 15, 20, 25, 30, 35, 40, 45, 50, 55};
        int[] titleList = new int[] {2, 11, 18, 23, 28, 33, 38, 43, 48, 53};

        String format = DateFormatUtils.format(new Date(), "yyyy-MM-dd");
        int month = Integer.parseInt(format.substring(5, 7));
        int day = Integer.parseInt(format.substring(8, 10));

        String format1 = DateFormatUtils.format(new Date(), "yyyy-MM");
        int _month = new Date().getMonth();
        _month = _month + 1;
        while (month > 0) {
            if (_month == month) {
                for (int i = day; i > 0; i--) {
                    for (int j = 0; j < indexList.length; j++) {
                        foreignExchangeService.parsePage(doc, titleList[j], indexList[j], format1 + "-" + i);
                    }
                }
            } else {
                format1 = format1.substring(0, 6) + month;
                for (int i = 30; i > 0; i--) {
                    for (int j = 0; j < indexList.length; j++) {
                        foreignExchangeService.parsePage(doc, titleList[j], indexList[j], format1 + "-" + i);
                    }
                }
            }
            month--;
        }

        int _12month = 12;
        while (_12month > 0) {
            if (_12month < 10) {
                format1 = "2022-0" + _12month;
            } else {
                format1 = "2022-" + _12month;
            }
            for (int i = 30; i > 0; i--) {
                for (int j = 0; j < indexList.length; j++) {
                    foreignExchangeService.parsePage(doc, titleList[j], indexList[j], format1 + "-" + i);
                }
            }
            _12month--;
        }

        collects = foreignExchangeService.queryAll();

        model.addAttribute("collects", collects);

        return "index";
    }

    @ResponseBody @GetMapping("/export/csv") public String export() throws Exception {

        List<ForeignExchangeEntity> collects = foreignExchangeService.queryAll();
        if (CollectionUtils.isEmpty(collects)) {
            return "文件无数据可导出！！！";
        }

        String name = UUID.randomUUID().toString().substring(0, 5);
        String path = "D:/" + name + ".csv";
        CSVUtils.exportCsv(path, new String[] {"交易场所", "入金门槛", "最小单位", "杠杆", "日期"},
            new String[] {"title", "startPrice", "minUnit", "gangGan", "createAt"}, collects);
        return "文件导出成功！！！导出目录：" + path;
    }
}
